Skip to content

Conversation

@rkistner
Copy link
Contributor

@rkistner rkistner commented Jun 17, 2025

Handle "Topology is closed" errors by exiting the process, in two ways:

  1. Explicitly call client.connect() on startup, allowing us to catch the error.
  2. Catch topologyClosed errors after startup, in case one does get through.

These errors are not recoverable - it does not get resolved by retrying. It typically only happens on start-up - if the connection is lost at a later point, we get a different error that is automatically retried. By exiting the process, the container management system such as Kubernetes or Docker Compose can handle restarting the process using an appropriate back-off.

Some other options to consider:

  1. Handling via a liveness probe: This could have the same effect, but would delay the exit for no good reason.
  2. Re-creating the MongoClient. We can consider this option in the future, but it is more complex to implement.

This primarily impacts the API process - the replication process already had similar behavior on connection errors during startup.

The does not affect connections to the source database - only to the storage database. Source database errors in replication are handled in the replication loop. Source database errors in the API process is much less significant - only affects write checkpoints. We can investigate better ways to handle that later, but I haven't seen any reports of that being an issue yet.

@changeset-bot
Copy link

changeset-bot bot commented Jun 17, 2025

🦋 Changeset detected

Latest commit: ed313da

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
@powersync/service-module-mongodb-storage Patch
@powersync/service-errors Patch
@powersync/service-core Patch
@powersync/lib-services-framework Patch
@powersync/lib-service-mongodb Patch
@powersync/service-schema Patch
@powersync/service-module-mongodb Patch
@powersync/service-module-mysql Patch
@powersync/service-module-postgres Patch
@powersync/service-image Patch
@powersync/service-core-tests Patch
@powersync/service-module-core Patch
@powersync/service-module-postgres-storage Patch
test-client Patch
@powersync/service-rsocket-router Patch
@powersync/lib-service-postgres Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@rkistner rkistner requested a review from Copilot June 17, 2025 11:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds handling for unrecoverable MongoDB "topology is closed" errors by explicitly connecting on startup and propagating fatal errors to gracefully exit the process with custom exit codes.

  • Introduces a new error code (PSYNC_S2402) for MongoDB connection failures.
  • Registers fatal error listeners in the storage engine and storage provider.
  • Updates process exit codes and corresponding log messages in CLI entry and lifecycle management.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/service-errors/src/codes.ts Added new error code PSYNC_S2402 for MongoDB connection failures.
packages/service-core/src/system/ServiceContext.ts Registered a listener to propagate storage fatal errors.
packages/service-core/src/storage/StorageProvider.ts Extended the ActiveStorage interface to include onFatalError callback.
packages/service-core/src/storage/StorageEngine.ts Hooked up fatal error notification from the active storage.
packages/service-core/src/entry/cli-entry.ts Changed error log and exit code to indicate fatal startup errors.
modules/module-postgres/src/replication/WalStream.ts Updated logger usage to use the instance logger.
modules/module-mongodb-storage/src/storage/implementation/MongoStorageProvider.ts Explicitly connects to MongoDB on startup and handles 'topologyClosed' errors.
libs/lib-services/src/system/LifeCycledSystem.ts Added stopWithError method with a distinct exit code for fatal errors.
.changeset/yellow-icons-cross.md Updated the changeset for affected packages.

@rkistner rkistner marked this pull request as ready for review June 17, 2025 11:50
@rkistner rkistner requested a review from stevensJourney June 17, 2025 11:50
Copy link
Collaborator

@stevensJourney stevensJourney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me :)

@rkistner rkistner merged commit f9e8673 into main Jun 17, 2025
35 of 36 checks passed
@rkistner rkistner deleted the topology-is-closed branch June 17, 2025 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants